fix(missing-user-stats): missing user stats rows covered via migration#1409
fix(missing-user-stats): missing user stats rows covered via migration#1409icecrasher321 merged 1 commit intostagingfrom
Conversation
There was a problem hiding this comment.
Greptile Summary
This PR adds a database migration (0091_backfill_user_stats.sql) that addresses a data integrity issue by creating missing user_stats records for existing users. The migration uses an INSERT SELECT statement with a LEFT JOIN to identify users who don't have corresponding entries in the user_stats table and creates them with default values.
The migration reveals that the user_stats table schema has evolved significantly since its original creation in migration 0021. While the original table had basic columns like total_manual_executions, total_api_calls, and total_cost, this backfill migration shows the table now includes many additional fields for copilot tracking (total_copilot_cost, total_copilot_tokens, total_copilot_calls), billing periods (current_period_cost, last_period_cost), usage limits (current_usage_limit, usage_limit_updated_at), and administrative controls (billing_blocked).
The migration addresses a common scenario where user creation and stats initialization might not happen atomically, leaving some users without proper tracking records. This is particularly important for billing and usage monitoring systems where every user needs comprehensive statistics tracking. The migration ensures all existing users get the same baseline stats configuration, setting most counters to 0, costs to '0' (decimal), timestamps to NOW(), and boolean flags to FALSE.
Confidence score: 4/5
- This migration is safe to run with low risk of causing immediate issues
- Score reflects well-structured SQL with proper NULL checking, though some design choices could be cleaner
- Pay attention to the mixed data type handling and potential confusion from using user.id for both id and user_id fields
1 file reviewed, 1 comment
| '0'::decimal AS total_cost, | ||
| '0'::decimal AS current_period_cost, | ||
| '0'::decimal AS last_period_cost, | ||
| '0'::decimal AS total_copilot_cost, |
There was a problem hiding this comment.
style: Inconsistent decimal casting - some use '0'::decimal while current_usage_limit uses NULL::decimal. Consider using consistent casting for all decimal fields.
Summary
Just add missing user stats rows via migration
Type of Change
Testing
Manually by running migration
Checklist